<?php

namespace App\Repository\Repositories;

use App\Http\Controllers\Controller;
use App\Models\StoreCombination;
use App\Models\StorePink;
use App\Models\StoreProduct;
use App\Repository\RepositoryInterface\GroupInterface;
use Illuminate\Database\Eloquent\Model;


class GroupRepositories extends Controller implements GroupInterface
{
    public function HeadGroupList()
    {
        $comb=new StoreCombination();
        $pink=new StorePink();
        $list=StorePink::with(['GroupProduct'])->where('k_id','=',0)->get()->toArray();
        return $list;
    }


    /**
     * 拼团 加入
     * @param $uid
     * @param $gid
     * @param $cid
     * @param $kid
     * @param $num
     * @return \Illuminate\Http\JsonResponse
     */
    public function JoinGroup($uid,$gid,$cid,$kid,$num)
    {
        $now_time = date('Y-m-d H:i:s');
        $pink = new StorePink();
        $pink_res = $pink->IsJoin($uid,$gid,$cid,$kid);
        if($pink_res)
        {
            return  parent::error('您已是该团成员');
        }
        $cmob = new StoreCombination();
        $res=$cmob->GroupGoods($cid);
        $c_people=$res['people'];
        $counta=$pink->GroupCount($cid,$kid);
        $count= count($counta);
        $expire_time = date('Y-m-d h:i:s',strtotime('+1 day'));
        if($c_people==$count)
        {
            return  parent::error('该团已满');
        }
        $array = [
            'uid'=>$uid,
            'order_id'=>'12313123',//暂定
            'order_id_key'=>'123123123',//暂定
            'total_num'=>$num,
            'total_price'=>$res['price']*$num,
            'cid' => $res['id'],
            'pid' => $res['id'],
            'people' => 1,
            'price' => $res['price'],
            'add_time'=>$res['start_time'],
            'stop_time'=>$res['stop_time'],
            'k_id'=>$kid,
            'is_tpl'=>0,
            'is_refund'=>0,
            'status'=>1
        ];
        try {
            $res=$pink->AddPink($array);
            if($res)
            {
                return parent::success('success');
            }
        }catch (\Exception $exception)
        {
            return  parent::error($exception->getMessage());
        }
    }


    /**
     * 用户退出拼团
     * @param $uid
     * @param $kid
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     */
    public function UserExitGroup($uid,$kid,$cid)
    {
        $pink=new StorePink();
        $res=$pink->UserExitGroup($uid,$kid,$cid);
        if($res)
        {
            return parent::success('success');
        }
    }


    /**
     * 团长取消开团
     * @param $uid
     * @param $cid
     * @return \Illuminate\Http\JsonResponse
     */
    public function HeadExitGroup($uid,$cid)
    {
        $pink=new StorePink();
        $res=$pink->HeadExitGroup($uid,$cid);
        if($res)
        {
            return parent::success('取消拼团成功');
        }
        else
        {
            return parent::error('取消拼团失败');
        }
    }



    /**
     * 团长拼团商品推荐查询
     * @return \Illuminate\Http\JsonResponse
     */
    public function recommendGoods(){
        try {
            $res = StoreCombination::recommendGoods();
            return parent::success("查询成功",$res);
        }catch (\Exception $exception) {
            return parent::error($exception->getMessage());
        }
    }


    /**
     * 用户发起拼团
     * @param $uid
     * @param $cid
     * @param $num
     * @return \Illuminate\Http\JsonResponse
     */
    public function UserInitGroup($uid,$cid,$num){
        $StorePink = new StorePink();
        $comb = new StoreCombination();
        $res = $StorePink->IfGroupHas($cid,$uid);
        $now = date('Y-m-d H:i:s');
        $expire_time = date('Y-m-d h:i:s',strtotime('+1 day'));
        if(!empty($res))
        {
            return parent::error('您已经对该商品发起拼团');
        }
        $g_goods = $comb -> GroupGoods($cid);
        $data = [
            'uid' => $uid,
            'order_id' => '123123',//暂定
            'order_id_key' => '123123',//暂定
            'total_num' => $g_goods['num'],
            'total_price' => $g_goods['price']*$num,
            'cid' => $g_goods['id'],
            'pid' => $g_goods['product_id'],
            'people' => 1,
            'price' => $g_goods['price'],
            'add_time'=> $now,
            'stop_time'=> $expire_time,
            'k_id' => 0,
            'is_tpl' => 0,
            'is_refund' => 0,
            'status' => 1
        ];
        try {
            $res=$StorePink->UserInitGroup($data);
            if($res)
            {
                return parent::success('发起拼团成功');
            }
            else
            {
                throw new \Exception('拼团发起失败');
            }
        }catch (\Exception $exception)
        {
            return parent::error($exception->getMessage());
        }
    }


    /**
     * hsx
     * 拼团列表
     * @param $size
     * @param $status
     * @return \Illuminate\Http\JsonResponse
     */
    public function groupList($size,$status)
    {
        try
        {
            $result = StorePink::storePinkWithUserWithStoreCombination($size,$status);

            return parent::success('success',$result);

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }
    }


    /**
     * hsx
     * 添加拼团
     * @param $post
     * @return \Illuminate\Http\JsonResponse
     */
    public function addGroup($post)
    {
        try
        {
            $data = [
                'uid' => $post['uid'],
                'order_id'=>time().rand(1111,9999),
                'total_num'=>$post['total_num'],
                'order_id_key'=>$post['order_id_key'],
                'total_price'=>$post['total_price'],
                'cid'=>$post['cid'],
                'pid'=>$post['pid'],
                'people'=>$post['people'],
                'price'=>$post['price'],
                'add_time'=>$post['add_time'],
                'stop_time'=>$post['stop_time'],
                'is_tpl'=>$post['is_tpl'],
                'is_refund'=>$post['is_refund'],
                'status'=>$post['status']
            ];

            StorePink::addStorePink($data);

            return parent::success('添加成功');

        }catch (\Exception $exception){

            return parent::success($exception->getMessage());

        }
    }


    /**
     * hsx
     * 删除拼团
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delGroup($id)
    {
        try
        {
            $find = StorePink::findStorePink($id);

            if ($find['status'] == 1){

                return parent::error('活动正在进行中不能删除');

            }
            if ($find['status'] == 3 && $find['is_refund'] == 0){

                return parent::error('活动未完成,还没退款');

            }

            StorePink::delStorePink($id);

            return parent::success('success');

        }catch (\Exception $exception){

            return parent::success($exception->getMessage());

        }
    }


    /**
     * 拼团修改
     * @param $post
     * @return \Illuminate\Http\JsonResponse
     */
    public function groupAlter($post)
    {
        try
        {

            $find = StorePink::findStorePink($post['id']);
            if ($find['status'] == 1){

                return parent::error('活动正在进行中');

            }

            $data = [
                'total_num'=>$post['total_num'],
                'total_price'=>$post['total_price'],
                'people'=>$post['people'],
                'price'=>$post['price'],
                'add_time'=>$post['add_time'],
                'stop_time'=>$post['stop_time'],
                'is_tpl'=>$post['is_tpl'],
            ];

            StorePink::storePinkAlter($post['id'],$data);

            return parent::success('success');

        }catch (\Exception $exception){

            return parent::success($exception->getMessage());

        }
    }


    /**
     * 拼团详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function groupDetails($id)
    {
        try
        {
            $find = StorePink::findStorePink($id);

            return parent::success('success',$find);


        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }
    }


    /**
     * 拼团商品列表
     * @param $size
     * @param $is_show
     * @param $title
     * @return \Illuminate\Http\JsonResponse
     */
    public function CollageGoodsList($size,$is_show,$title)
    {
        try
        {
            $result = StoreCombination::storeCombinationWithStoreProductWithUser($size,$is_show,$title);

            foreach ($result as $val){
                $val['stop_time'] = date('Y-m-d H:i:s',$val['stop_time']);
            }

            return parent::success('success',$result);

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }

    }


    /**
     * hsx
     * 拼团商品添加
     * @param $post
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCollageGoods($post)
    {
        try
        {

            //查询商品
            $storeProduct = StoreProduct::find($post['product_id']);

            $data = [
                'product_id'=>$post['product_id'],
                'image'=>$storeProduct['image'],
                'images'=>$storeProduct['slider_image'],
                'title'=>$post['title'],
                'attr'=>111111,
                'people'=>$post['people'],
                'info'=>$post['info'],
                'price'=>$post['price'],
                'sort'=>$post['sort'],
                'sales'=>$storeProduct['sales'],
                'stock'=>$storeProduct['stock'],
                'add_time'=>$storeProduct['add_time'],
                'is_host'=>$storeProduct['is_host'],
                'is_show'=>$storeProduct['is_show'],
                'is_del'=>$storeProduct['is_del'],
                'mer_use'=>$storeProduct['mer_use'],
                'is_postage'=>$storeProduct['is_postage'],
                'postage'=>$storeProduct['postage'],
                'start_time'=>strtotime($post['date_time'][0]),
                'stop_time'=>strtotime($post['date_time'][1]),
                'effective_time'=>$post['effective_time'],
                'cost'=>$storeProduct['cost'],
                'browse'=>$storeProduct['browse'],
                'unit_name'=>$storeProduct['unit_name'],
                'temp_id'=>$post['temp_id'],
                'num'=>$post['num'],
                'quota'=>$storeProduct['stock'],
                'quota_show'=>$storeProduct['stock'],
            ];

            StoreCombination::add($data);

            return parent::success('success');

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }

    }


    /**
     * hsx
     * 删除拼团商品
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCollageGoods($id)
    {
        try
        {
            StoreCombination::del($id);

            return parent::success('success');

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }

    }


    /**
     * hsx
     * 拼团商品详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detailsCollageGoods($id)
    {
        try
        {
            $find = StoreCombination::find($id);

            return parent::success('success',$find);

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }

    }


    /**
     * hsx
     * 拼团商品修改
     * @param $post
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCollageGoods($post)
    {
        try
        {

            //查询商品
            $storeProducts = StoreProduct::find($post['product_id']);

            $res = [
                'product_id'=>$post['product_id'],//商品ID
                'image'=>$storeProducts['image'],//商品图片
                'images'=>$storeProducts['slider_image'],//商品轮播图
                'title'=>$post['title'],//活动图片
                'attr'=>111111,
                'people'=>$post['people'],
                'info'=>$post['info'],
                'price'=>$post['price'],
                'sort'=>$post['sort'],
                'sales'=>$storeProducts['sales'],
                'stock'=>$storeProducts['stock'],
                'add_time'=>$storeProducts['add_time'],
                'is_host'=>$storeProducts['is_host'],
                'is_show'=>$storeProducts['is_show'],
                'is_del'=>$storeProducts['is_del'],
                'mer_use'=>$storeProducts['mer_use'],
                'is_postage'=>$storeProducts['is_postage'],
                'postage'=>$storeProducts['postage'],
                'start_time'=>strtotime($post['date_time'][0]),
                'stop_time'=>strtotime($post['date_time'][1]),
                'effective_time'=>$post['effective_time'],
                'cost'=>$storeProducts['cost'],
                'browse'=>$storeProducts['browse'],
                'unit_name'=>$storeProducts['unit_name'],
                'temp_id'=>$post['temp_id'],
                'num'=>$post['num'],
                'quota'=>$storeProducts['stock'],
                'quota_show'=>$storeProducts['stock'],
            ];

            StoreCombination::alter($post['id'],$res);

            return parent::success('更新成功');

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }

    }


    /**
     * hsx
     * 修改拼团状态
     * @param $id
     * @param $is_show
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeDiscountStatus($id, $is_show)
    {
        try
        {
            $find = StoreCombination::find($id);

            if ($find['is_show'] == 1){

                $is_show = 2;

            }else{

                $is_show = 1;

            }

            StoreCombination::where('id',$id)->update(['is_show'=>$is_show]);

            return parent::success('success');

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }
    }


    /**
     * hsx
     * 选择商品
     * @return \Illuminate\Http\JsonResponse
     */
    public function selectStoreProduct()
    {
        try
        {

            $result = StoreProduct::getStoreProduct();

            return parent::success('success',$result);

        }catch (\Exception $exception){

            return parent::error($exception->getMessage());

        }
    }


    /**
     * 拼团商品列表查询
     * @return \Illuminate\Http\JsonResponse
     */
    public function combinationList(){
        try {
            $combination = (new StoreCombination())::combinationList();
            return parent::success("查询成功",$combination);
        } catch (\Exception $exception){
            return parent::error($exception->getMessage());
        }
    }


}
